<?php
/**
 *
 *
 * @package    channel 
 * @author     qiulf <qiulf@gmail.com>
 * @copyright  2009 fengone.com
 * @created    2010-07-25 16:11:35.
 * @version    $Id: eub.class.php 2908 2013-04-10 10:43:06Z qiulf $
 * SVNPath     $HeadURL: http://svn.ladyol.cn/fcms/branches/wj/www.mubook.com/libs/core/channel/eub.class.php $         
 */

//defined('APP') || exit('Access Denied!');

class Eub {
    
    const METHOD_POST = "POST";

    const METHOD_GET = "GET";

    const METHOD_DELETE = "DELETE";
    /**
     *服务地址
     */
    private $baseUrl = "http://www.ems.com.cn/partner/";

    /**
     *发运订单地址
     *SHIP_ORDER + xml
     */
    const SHIP_ORDER = "api/public/p/order/";

    /**
     *验证发运订单地址
     */
    const VALIDATE_URL = "api/public/p/validate/";

    /**
     *运单信息取消
     *DELETE_MAILNUM_URL + mailnum
     */
    const DELETE_MAILNUM_URL = "api/public/p/order/";

    /**
     *国际e邮运单跟踪信息服务
     *EU_TRACK + mailnum
     */
    const EU_TRACK = "api/public/p/track/query/cn/";

    /**
     *国际e邮宝费率计算
     */
    const EU_RATE = "api/public/p/rate/epacket/";

    /**
     *国际e邮宝获取国内省级行政区划
     */
    const EU_PROVINCE = "api/public/p/area/cn/province/list/";

    /**
     *国际e邮宝获取省份内城市行政区划
     *EU_CITY + code
     */
    const EU_CITY = "api/public/p/area/cn/city/list/";

    /**
     *国际e邮宝获取城市下辖县区
     *EU_COUNTRY + code
     */
    const EU_COUNTRY = "api/public/p/area/cn/county/list/";

    /**
     *请求各个服务地址
     */
    private $url = "";

    /**
     *请求的方法
     */
    private $method = "";

    /**
     *POST请求的请求体
     */
    private $content = "";

    /**
     *编码
     */
    private $encode = "";

    /**
     *保存请求的头信息
     */
    private $headers = array(
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "version: international_eub_us_1.1",
        "authenticate: Taomore_0c26d80474263e1b98da1d18f60327f5"
    );

    /**
     *保存请求返回的头信息
     */
    private $backHeaders = array();

    public function __construct($url, $method, $content = "", $encode = "UTF-8") {
        $this->url = $this->baseUrl . $url;
        $this->method = $method;
        $this->content = $content;
        $this->encode = $encode;
    }

    public function getBaseUrl() {
        return $this->baseUrl;
    }

    public function setBaseUrl(string $baseUrl) {
        $this->baseUrl = $baseUrl;
    }

    public function getUrl() {
        return $this->url;
    }

    public function setUrl(string $url) {
        $this->url = $url;
    }

    public function getMethod() {
        return $this->method;
    }

    public function setMethod(string $method) {
        $this->method = $method;
    }

    public function getContent() {
        return $this->content;
    }

    public function setContent(string $content) {
        $this->content = $content;
    }

    public function setBackHeaders($headers = "") {
        foreach ($headers as $value) {
            $t = explode (': ', $value);
            $k = isset($t[0]) ? $t[0] : "";
            $v = isset($t[1]) ? $t[1] : "";
            if (array_key_exists($k, $this->backHeaders)) {
                $this->backHeaders[$k] = $this->backHeaders[$k] . "," . $v;
            } else {
                $this->backHeaders[$k] = $v;
            }
        }
    }

    public function _http($method = "POST") {
        $url = $this->url;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        if ($method == Eub::METHOD_POST) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->content);
        }
        curl_setopt($ch, CURLOPT_HEADER, true); 

        $response = "";
        $response = curl_exec($ch);

		//$info = curl_getinfo($ch);
        //var_dump($info);
		//$erron = curl_errno($ch);
        //var_dump($erron);
        curl_close($ch);

        $count = explode("\r\n\r\n", $response);
        if (count($count) == 2) list($other, $responseBody) = $count;
        if (count($count) == 3) list($c, $other, $responseBody) = $count;
        $other = preg_split("/\r\n|\n|\r/", $other);
        $this->setBackHeaders($other);

        return $responseBody;
    }

    private function _getParametersAsString(array $parameters) {
        $queryParameters = array();
        foreach ($parameters as $key => $value) {
            $queryParameters[] = $key . '=' . $this->_urlencode($value);
        }
        return implode('&', $queryParameters);
    }

    private function _urlencode($value) {
		return str_replace('%7E', '~', rawurlencode($value));
    }
}
